<script>
export default {
  props: {
    icon: String,
    msg: String,
    duration: Number,
    top: Number,
    timestamp: Number,
  },
  data () {
    return {
      isAnimationIn: true,
      isAnimationOut: false,
      isHidden: false,
    }
  },
  created () {
    this.autoCloseTimer = null
    this.hiddenTimer = null
    this.closeAnimateInTimer = null
  },
  mounted () {
    this.autoCloseToast()
    this.closeAnimateInTimer = setTimeout(() => {
      clearTimeout(this.closeAnimateInTimer)
      this.isAnimationIn = false
    }, 200)
  },
  beforeDestroy () {
    clearTimeout(this.autoCloseTimer)
    clearTimeout(this.hiddenTimer)
    this.$el.parentNode.removeChild(this.$el)
  },
  methods: {
    autoCloseToast () {
      let duration = this.duration
      this.autoCloseTimer = setTimeout(() => {
        clearTimeout(this.autoCloseTimer)
        this.isAnimationOut = true
        this.hiddenTimer = setTimeout(() => {
          clearTimeout(this.hiddenTimer)
          this.isHidden = true
          this.$nextTick(() => {
            this.$emit('close')
          })
        }, 150)
      }, 1e3 * (duration - 0.15))
    },
    handleHoverEnter () {
      clearTimeout(this.autoCloseTimer)
      clearTimeout(this.hiddenTimer)
      this.isAnimationIn = false
      this.isAnimationOut = false
      this.isHidden = false
    },
    handleHoverLeave () {
      this.autoCloseToast()
    },
  },
  render () {
    let msg = this.msg
    let icon = this.icon
    let isHidden = this.isHidden
    let isAnimationIn = this.isAnimationIn
    let isAnimationOut = this.isAnimationOut
    let className = 'component-toast-content'
    isAnimationIn && (className += ' animation-fade-in-down')
    isAnimationOut && (className += ' animation-fade-out-up')
    isHidden && (className += ' hidden')
    return (
      <div
        style={{ top: this.top + 'px' }}
        class={className}
        on-mouseenter={this.handleHoverEnter}
        on-mouseleave={this.handleHoverLeave}
        on-touchstart={this.handleHoverEnter}
        on-touchend={this.handleHoverLeave}
      >
        <i class={['toast-icon', icon]}></i>
        <span>{msg}</span>
      </div>
    )
  },
}
</script>

<style>
.component-toast {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 0;
  text-align: center;
}

.component-toast .component-toast-content {
  position: relative;
  display: inline-flex;
  align-items: center;
  max-width: 98%;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(56, 56, 56, 0.2);
  border-radius: 14px;
  padding: 15px 20px;
  font-size: 14px;
}

.component-toast .component-toast-content .toast-icon {
  margin-right: 10px;
  font-size: 18px;
}

.component-toast .component-toast-content .toast-icon.el-icon-success {
  color: #73D13D;
}

.component-toast .component-toast-content .toast-icon.el-icon-error {
  color: #FF4A4A;
}

.component-toast .component-toast-content .toast-icon.el-icon-info {
  color: #3C77FE;
}

.component-toast .component-toast-content .toast-icon.el-icon-warning {
  color: #FFA900;
}

.component-toast .component-toast-content span {
  font-size: 12px;
  line-height: 16px;
  white-space: pre-wrap;
}

</style>
